//
//  YoLoadingHelpView.m
//  DawnHealth
//
//  Created by hrrMac on 2019/12/10.
//  Copyright © 2019 hheTeamiOS. All rights reserved.
//

#import "YoLoadingHelpView.h"
#import "FLAnimatedImage.h"

@interface YoLoadingHelpView()

@property (nonatomic,assign)int w;

@property (nonatomic,assign)int h;

///背景视图
@property (strong, nonatomic) UIView *backgroundView;
///GIF动画视图
@property (strong, nonatomic)  FLAnimatedImageView *gifImgView;
///空界面或者网络错误视图
@property (strong, nonatomic) UIImageView *emptyImgView;
///需要操作的按钮视图
@property (strong, nonatomic) UIButton *btn;
///空界面提示或者网络错误提示
@property (strong, nonatomic) UILabel *lab;

@property (assign, nonatomic) LoadingHelpType type;

@end


@implementation YoLoadingHelpView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.w=frame.size.width;
        self.h=frame.size.height;
        [self loadingHelpInit];
    }
    return self;
}

///初始化背景视图
-(void)loadingHelpInit{
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.backgroundView];
    self.backgroundView.frame = CGRectMake(0, 0, self.w, self.h);
    
    [self addSubview:self.gifImgView];
    [self.gifImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.centerY.mas_offset(-40);
        make.size.height.width.mas_offset(300);
    }];
    
    [self addSubview:self.gifImgView];
    [self addSubview:self.lab];
    [self addSubview:self.emptyImgView];
    [self addSubview:self.btn];
    
//    self.emptyImgView.frame = CGRectMake(30, 200, 80, 80);
//    self.lab.frame = CGRectMake(10, self.h / 2 , self.w - 20, 20);
    
    [self.lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
        make.centerY.mas_offset(0);
    }];
    [self.emptyImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.lab.mas_top).mas_offset(-20);
        make.centerX.mas_offset(0);
        make.size.mas_offset(CGSizeMake(246, 185));
    }];
    
    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lab).mas_offset(30);
        make.centerX.equalTo(self.lab);
        make.size.mas_offset(CGSizeMake(81, 24));
    }];
}


///加载loading挡板动画
-(void)loadGif{
    NSURL *url1 = [[NSBundle mainBundle] URLForResource:@"loading" withExtension:@"gif"];
    NSData *data1 = [NSData dataWithContentsOfURL:url1];
    FLAnimatedImage *animatedImage1 = [FLAnimatedImage animatedImageWithGIFData:data1];
    self.gifImgView.animatedImage = animatedImage1;
}


-(void)viewContentOffSet:(float)y{
    [self.lab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_offset(y);
    }];
}


#pragma mark - function
///loading加载工具
-(void)showLoadingHelp{
    [self hiddenGif:NO];
    [self loadGif];
}

///展示空视图
-(void)showEmpty{
    
}


-(void)showEmptyAndError:(LoadingHelpType)type{
    _type = type;
    switch (type) {
        case LoadingHelpTypeNetWorkBtn:
            [self showEmpty:@"暂无网络" withImg:@"error_network_no" withBtnTitle:@"点击重试"];
        break;
            
        case LoadingHelpTypeCoupons:
            [self showEmpty:@"暂无可用优惠券" withImg:@"empty_coupon_no" withBtnTitle:@""];
        break;
        
        case LoadingHelpTypeAddressBtn:
            [self showEmpty:@"您还没有收藏收货地址哦~" withImg:@"empty_address_no" withBtnTitle:@"新建地址"];
        break;
        
        case LoadingHelpTypeOrder:
            [self showEmpty:@"暂无订单" withImg:@"empty_order_no" withBtnTitle:@""];
        break;
        
        case LoadingHelpTypeConcer:
            [self showEmpty:@"暂无关注" withImg:@"empty_corcern_no" withBtnTitle:@""];
        break;
        
        case LoadingHelpTypeCollectionBtn:
            [self showEmpty:@"您还没有收藏，快去逛逛吧~" withImg:@"empty_collection_no" withBtnTitle:@"去逛逛"];
        break;
        
        case LoadingHelpTypeNormal:
            self.emptyImgView.contentMode = UIViewContentModeBottom;
            [self showEmpty:@"暂无地址" withImg:@"noDate" withBtnTitle:@""];
        break;
            
        case LoadingHelpTypeErrorData:
            [self showEmpty:@"数据错误" withImg:@"error_network_no" withBtnTitle:@"点击重试"];
        break;
            
        case LoadingHelpTypeNOAnswer:
            [self showEmpty:@"暂无回答" withImg:@"empty_no_answer" withBtnTitle:@""];
        break;
            
        default:
            [self showEmpty:@"数据错误" withImg:@"error_network_no" withBtnTitle:@"点击重试"];
        break;
    }
    
}

-(void)yoShowEmpty{
    [self showEmptyAndError:(LoadingHelpTypeNormal)];
}


-(void)showEmpty:(NSString *)emptyStr withImg:(NSString *)img{
    [self showEmpty:emptyStr withImg:img withBtnTitle:@""];
}


-(void)showEmpty:(NSString *)emptyStr withImg:(NSString *)img withBtnTitle:(NSString *)btnTitle{
    [self hiddenGif:YES];
    self.emptyImgView.image = [UIImage imageNamed:img];
    self.lab.text = emptyStr;
    if (btnTitle.length == 0) {
        self.btn.hidden = YES;
    }else{
        self.btn.hidden = NO;
        [self.btn setTitle:btnTitle forState:(UIControlStateNormal)];
    }
}

///隐藏loading还是空白视图
-(void)hiddenGif:(BOOL)hidden{
    self.gifImgView.hidden = hidden;
    self.btn.hidden = !hidden;
    self.lab.hidden = !hidden;
    self.emptyImgView.hidden = !hidden;
    self.hidden = NO;
    if(hidden){
        self.gifImgView.image = nil;
    }
}


#pragma mark - action
-(void)btnClick{
    ///网络错误按钮点击的回调
    if (_type == LoadingHelpTypeNetWorkBtn) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(loadingHelpError)]) {
            [self.delegate loadingHelpError];
        }
        return;
    }
    ///其他功能按钮的回调
    if (self.delegate && [self.delegate respondsToSelector:@selector(loadingHelpBtnClick)]) {
        if(self.delegate && [self.delegate respondsToSelector:@selector(loadingHelpBtnClick)]){
            [self.delegate loadingHelpBtnClick];
        }
        
    }
}


#pragma mark - setter and getter

-(UIView *)backgroundView{
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc]init];
    }
    return _backgroundView;
}


-(FLAnimatedImageView *)gifImgView{
    if (!_gifImgView) {
        _gifImgView = [[FLAnimatedImageView alloc]init];
    }
    return _gifImgView;
}

-(UILabel *)lab{
    if (!_lab) {
        _lab = [[UILabel alloc]init];
        _lab.textColor = HEXCOLOR(@"#999999");
        _lab.font = [UIFont systemFontOfSize:15];
        _lab.textAlignment = NSTextAlignmentCenter;
        _lab.numberOfLines = 2;
    }
    return _lab;
}


-(UIButton *)btn{
    if (!_btn) {
        _btn = [[UIButton alloc]init];
        [_btn setTitleColor:HEXCOLOR(@"#3B9293") forState:(UIControlStateNormal)];
        _btn.layer.borderColor = HEXCOLOR(@"#3B9293").CGColor;
        _btn.layer.borderWidth = 0.5;
        _btn.layer.cornerRadius = 3;
        _btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_btn addTarget:self action:@selector(btnClick) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _btn;
}

-(UIImageView *)emptyImgView{
    if (!_emptyImgView) {
        _emptyImgView = [[UIImageView alloc]init];
        _emptyImgView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _emptyImgView;
}



@end
